//
//  JSTUserBirthdayCell.m
//  KYKJPackDemo
//
//  Created by 01 on 17/6/19.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTUserBirthdayCell.h"

@interface JSTUserBirthdayCell ()<JSTPickViewDelegate>

@end

@implementation JSTUserBirthdayCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _label = [[UILabel alloc] init];
        CGFloat labelX = EvaluationCellInset;
        CGFloat labelY = EvaluationCellInset*2;
        CGSize size = [NSString sizeWithString:@"生日:" font:kSystemFont(kCurrentScreenWidth(12)) CGsize:CGSizeMake(kDeviceWidth, MAXFLOAT)];
        _label.frame = (CGRect){{labelX, labelY}, size};
        _label.text = @"生日:";
        _label.font = kSystemFont(kCurrentScreenWidth(12));
        _label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_label];
        
//        _textFd = [[UITextField alloc] init];
//        [self addSubview:_textFd];
//        
//        _selectDateBtn = [[UIButton alloc] init];
//        [_textFd addSubview:_selectDateBtn];
    }
    return self;
}

- (void) createContent:(NSString *)str blockNewStr:(void (^)(NSString *))newStr
{
    CGFloat labelX = EvaluationCellInset;
    CGFloat labelY = EvaluationCellInset*2;
    CGSize size = [NSString sizeWithString:@"生日:" font:kSystemFont(kCurrentScreenWidth(12)) CGsize:CGSizeMake(kDeviceWidth, MAXFLOAT)];
    CGSize maxSize = [NSString sizeWithString:@"生日生日:" font:kSystemFont(kCurrentScreenWidth(12)) CGsize:CGSizeMake(kDeviceWidth, MAXFLOAT)];
    _label.frame = (CGRect){{labelX, labelY}, size};
    _label.text = @"生日:";
    _label.font = kSystemFont(kCurrentScreenWidth(12));
    _label.textAlignment = NSTextAlignmentCenter;
    
    if ([str isEqualToString:@"/"]) {
        str = @"";
    }
    CGFloat textFdX = CGRectGetMaxX(_label.frame) + maxSize.width - size.width + EvaluationCellInset;
    CGFloat textFdY = 0;
    CGFloat textFdW = kDeviceWidth - textFdX - EvaluationCellInset;
    CGFloat textFdH = CGRectGetMaxY(_label.frame) + EvaluationCellInset*2;
    _textFd.frame = CGRectMake(textFdX, textFdY, textFdW, textFdH);
    _textFd.placeholder = @"请选择";
    _textFd.text = str;
    _textFd.font = kSystemFont(kCurrentScreenWidth(12));
    
    _selectDateBtn.frame = _textFd.bounds;
    _selectDateBtn.backgroundColor = kClearColor;
    [_selectDateBtn addTarget:self action:@selector(showSelectDate) forControlEvents:UIControlEventTouchUpInside];
}

- (void) showSelectDate
{
    [self.pickerView showOnWindow];
    
    [[self currentController].view endEditing:YES];
}

- (UIViewController *) currentController
{
    for (UIView *nextView = self.superview ; nextView; nextView = nextView.superview)
    {
        UIResponder *nextResponder = [nextView nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
        {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (void)confirmBtnHaveClick:(JSTPickerView *)pickView resultString:(NSString *)resultString indexPath:(NSIndexPath *)indexPath
{
    [self.pickerView dismissView];
    _textFd.text = resultString;
}

- (JSTPickerView *)pickerView
{
    if (!_pickerView)
    {
        _pickerView  = [[JSTPickerView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight) string:nil getType:birthdayType];
        _pickerView.delegate = self;
    }
    return _pickerView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
